#ifndef __H_PACKETBASE_H__
#define __H_PACKETBASE_H__

#include "global.h"

#ifndef _WINDEF_
typedef unsigned char       BYTE;
typedef int            BY_INT;
#else
typedef int        BY_INT;
#endif

typedef unsigned int  UINT;
typedef UINT  uint32;
typedef unsigned long ULONG;
typedef unsigned short USHORT;
typedef USHORT *PUSHORT;
typedef unsigned char UCHAR;
typedef wchar_t WCHAR;    // wc,   16-bit UNICODE character
typedef unsigned short      WORD;
typedef WORD uint16;
typedef unsigned long       DWORD;
typedef unsigned long long _u64_;
typedef  long long int64;
#ifndef ICHAT_TCP_MIN_BUFFER    //TCP socket 缺省接收 Buffer Size
#    define    ICHAT_TCP_MIN_BUFFER    1024
#endif

#ifndef ICHAT_TCP_DEFAULT_BUFFER    //TCP socket 缺省接收 Buffer Size
#    define    ICHAT_TCP_DEFAULT_BUFFER    8192
#endif

#ifndef ICHAT_TCP_MAX_BUFFER    //TCP socket 最大接收 Buffer Size
#    define    ICHAT_TCP_MAX_BUFFER    1024*16
#endif

#ifndef ICHAT_UDP_DEFAULT_BUFFER    //TCP socket 缺省接收 Buffer Size
#    define    ICHAT_UDP_DEFAULT_BUFFER    1024*8
#endif
// 默认HTTP最大请求长度
#ifndef MAX_HTTP_REQUEST_SIZE
#    define        MAX_HTTP_REQUEST_SIZE        ICHAT_TCP_DEFAULT_BUFFER
#endif

// 默认HTTP最大Head个数
#ifndef MAX_HTTP_REQUEST_FIELDS
#    define        MAX_HTTP_REQUEST_FIELDS        100
#endif

#define de_htonll(x) \
    ({ \
     _u64_ __x = (x); \
     ((_u64_)( \
         (_u64_)(((_u64_)(__x) & (_u64_)0x00000000000000ffULL) << 56) | \
         (_u64_)(((_u64_)(__x) & (_u64_)0x000000000000ff00ULL) << 40) | \
         (_u64_)(((_u64_)(__x) & (_u64_)0x0000000000ff0000ULL) << 24) | \
         (_u64_)(((_u64_)(__x) & (_u64_)0x00000000ff000000ULL) <<  8) | \
         (_u64_)(((_u64_)(__x) & (_u64_)0x000000ff00000000ULL) >>  8) | \
         (_u64_)(((_u64_)(__x) & (_u64_)0x0000ff0000000000ULL) >> 24) | \
         (_u64_)(((_u64_)(__x) & (_u64_)0x00ff000000000000ULL) >> 40) | \
         (_u64_)(((_u64_)(__x) & (_u64_)0xff00000000000000ULL) >> 56) )); \
     })

inline long long htonll(long long hvalue)
{
    //判断本机是大端还是小端
    union _v_{
        unsigned short a;
        unsigned char b[2];
    }v;
    v.b[0] = 0x1;
    v.b[1] = 0x2;
    if( v.a == 0x12 ) //本地是大端
    {
        return hvalue;
    } 
    else //本地是小端，需要转换
    {
        return (long long)de_htonll(hvalue);
    }
}

inline long long ntohll(long long hvalue)
{
    //判断本机是大端还是小端
    union _v_{
        unsigned short a;
        unsigned char b[2];
    }v;
    v.b[0] = 0x1;
    v.b[1] = 0x2;
    if( v.a == 0x12 ) //本地是大端
    {
        return hvalue;
    }
    else //本地是小端，需要转换
    {
        return (long long)de_htonll(hvalue);
    }
}

class CCPacketBase
{
public:
	CCPacketBase(){}
	virtual ~CCPacketBase(){}

	virtual char *packet_buf(void) = 0;
	virtual int packet_size(void) = 0;
	virtual bool IsWritecbCheckCode(void) = 0;
	virtual int GetHeadLength(void) = 0;
	virtual int GetContentLength(void) = 0;
	virtual void WritecbCheckCode(BYTE nValue) = 0;
	virtual BYTE GetcbCheckCode(void) = 0;
	virtual void SetCheckCodeFlag(BYTE value) = 0;
};

#endif

